/**
 * Copyright (c) 2009, 3BitSoftware
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the 3BitSoftware nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY 3BitSoftware ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL 3BitSoftware BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.threebitsoftware.tbb.module.photo;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;
import org.threebitsoftware.tbb.module.photoservice.PhotoInterface;

/**
 * handle the contact's photo.  It have import and export photo functions and
 * a resize method to crop the photo to 50 by 50
 */
public class ContactPhoto implements PhotoInterface {
	
	private static final int WIDTH = 200;
	private static final int HEIGHT = 150;
	
	private static Logger photoLogger = Logger.getLogger(ContactPhoto.class);
	
	/**
	 * default constructor
	 */
	public ContactPhoto() {
		
	}
	
	/**
	 * 
	 * @param photoFilePath
	 * @return
	 */
	public byte[] getImportPhoto(String fullFileName) {
		byte[] photo = null;
		String fileExtension = fullFileName.substring(fullFileName.indexOf(".") + 1);
		try {
			File inPhoto = new File(fullFileName);
			BufferedImage bufContactPhoto = ImageIO.read(inPhoto);
			int imgType = bufContactPhoto.getType() == 0 ? 
					BufferedImage.TYPE_INT_ARGB : bufContactPhoto.getType();
			
			BufferedImage cropped = cropPhotoToSize(bufContactPhoto,imgType);
			
			ByteArrayOutputStream photoInByteArray = new ByteArrayOutputStream();
			ImageIO.write(cropped, fileExtension, photoInByteArray);
			photo = photoInByteArray.toByteArray();
			
		} catch (IOException ex) {
			photoLogger.error("Fail to read image from file destination:\n", ex);
		}
		return photo;
	}
	
	/**
	 * 
	 * @param currentContactsPhoto
	 */
	public void exportPhoto(byte[] currentContactsPhoto, String fullFileName) {

		try {
			InputStream in = new ByteArrayInputStream(currentContactsPhoto);
			BufferedImage imageFromByte = ImageIO.read(in);

			ImageIO.write(imageFromByte, "gif", new File(fullFileName)); 
		} catch (FileNotFoundException ex) {
			photoLogger.error("Invalid full file name:\n", ex);			
		} catch (IOException ex) {
			photoLogger.error("Fail to write image to file destination:\n", ex);
		}
	}
	/**
	 * 
	 * @param original
	 * @param imgType
	 * @return
	 */
	private BufferedImage cropPhotoToSize(BufferedImage original, int imgType){
		BufferedImage croppedImg = new BufferedImage(WIDTH, HEIGHT, imgType);
		Graphics2D g = croppedImg.createGraphics();
		g.drawImage(original, 0, 0, WIDTH, HEIGHT, null);
		g.dispose();
	 
		return croppedImg;
	}	
}
